package com.dianjing.dianjing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dianjing.dianjing.dto.input.LoginInput;
import com.dianjing.dianjing.dto.input.PasswordAlterInput;
import com.dianjing.dianjing.dto.input.UserInfoUpdateInput;
import com.dianjing.dianjing.dto.input.UserInput;
import com.dianjing.dianjing.dto.output.PainterShowOutput;
import com.dianjing.dianjing.dto.output.UserOutput;
import com.dianjing.dianjing.entity.User;
import com.dianjing.dianjing.enums.UserTypeEnum;
import com.dianjing.dianjing.service.UserService;
import com.dianjing.dianjing.util.DataUtils;
import com.dianjing.dianjing.util.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author owf
 * @Date 2022/11/8
 */
@Api(tags = "用户操作接口")
@RestController
@RequestMapping("/user")
public class UserController {



    @Resource
    private UserService userService;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public ResponseData login(@RequestBody LoginInput input) {
        return userService.login(input);
    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public ResponseData register(@RequestBody @Valid UserInput input) {
        return userService.register(input);
    }

    @GetMapping("/collectPainter/{painterId}")
    @ApiOperation("收集的画家")
    public ResponseData collectPainter(@PathVariable("painterId") int painterId) {
        // 获取用户收藏的画家
        List<UserOutput> painters =  userService.collectPainter(painterId);
        return ResponseData.success().data(painters);
    }


    @GetMapping("/myInfo/{userId}")
    @ApiOperation("获取用户的基本信息")
    public DataUtils myInfo(@PathVariable("userId") Long userId) {
        // 用户表记录item信息
        User user = userService.getById(userId);
        // 输出的信息
        UserOutput userOutput = new UserOutput();
        // 倒数据
        BeanUtils.copyProperties(user,userOutput);
        return DataUtils.ok(userOutput);
    }

    @PostMapping("/updateUserInfo")
    @ApiOperation("更新用户的基本信息")
    public DataUtils updateUserInfo(UserInfoUpdateInput userInfoUpdateInput) {
        Boolean isUpdateUserInfoSuccess = userService.updateUserInfo(userInfoUpdateInput);
        if(isUpdateUserInfoSuccess) {
            return DataUtils.ok();
        }
        return DataUtils.error("更新用户信息失败了");
    }

    @GetMapping("/seeTypePainters")
    @ApiOperation("获取指定类别画师信息-从其作品中获取是什么类别的信息")
    public DataUtils seeTypePainters(@RequestParam(value = "typeId",required = false) Long typeId) {

        List<PainterShowOutput> painterShowOutputs = userService.seeTypePainters(typeId);
        if(painterShowOutputs != null) {
            return DataUtils.ok(painterShowOutputs);
        }
        return DataUtils.error("获取指定类别画师信息失败了");
    }
    @PostMapping("/alterPassword")
    @ApiOperation("获取指定类别画师信息-从其作品中获取是什么类别的信息")
    public DataUtils alterPassword(@RequestBody PasswordAlterInput passwordAlterInput) {
         return userService.alterPassword(passwordAlterInput.getUserId(),passwordAlterInput.getOldPassword(),passwordAlterInput.getNewPassword());
    }






}
